package com.perceptivesoftware.licensing.client;

import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.MessageBox.MessageBoxType;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.StatusCodeException;


public abstract class LicensingAsyncCallback<T> implements AsyncCallback<T> {

	abstract public void onSuccess(final T result);

	public void onPerceptiveFailure(final Throwable caught)
	{
		final MessageBox mb = new MessageBox();
		mb.setTitle("Perceptive Licensing Server");
		mb.setType(MessageBoxType.ALERT);
		mb.setIcon(MessageBox.ERROR);
		mb.setMessage("An unknown error has occurred.");
		mb.setModal(true);
		mb.addCallback(new CallbackErrorAcknowledger());
		mb.show();
	}

	public void failureReturned(){

	}
	public void onFailure(final Throwable caught)
	{
		final MessageBox mb1 = new MessageBox();
		mb1.setTitle("Perceptive Licensing Server");
		mb1.setType(MessageBoxType.ALERT);
		mb1.setIcon(MessageBox.ERROR);
		mb1.setMessage(caught.getMessage());
		mb1.setModal(true);
		mb1.addCallback(new CallbackErrorAcknowledger());
		mb1.show();
		failureReturned();
		caught.printStackTrace();
		String exceptionMessage = null;
		try
		{
			throw caught;
		}
		catch (final IncompatibleRemoteServiceException e)
		{
			// this should be exceptionally rare for us; this could conceivably
			// happen if we were to drop a new war into a server when clients
			// were connected and using the system. On the very next RPC call
			// this exception would be thrown as the version of the client is
			// different than the version on the server.
			exceptionMessage = "The Perceptive Licensing server version is incompatible. Please close Licensing and log in again.";
	
		}
		catch (final StatusCodeException e)
		{
			// this type of exception will be thrown if the client cannot
			// reach the server within the RPC timeout. Unfortunately
			exceptionMessage = "The Perceptive Licensing server cannot be reached. Try again. " + "If this problem persists, contact your system administrator.";
		}
		catch (final InvocationException e)
		{
			onPerceptiveFailure(e);
		}
		catch (final Throwable e)
		{
			onPerceptiveFailure(e);
			return;
		}

		// no specific exception thrown;
		// parse for 403; session timeout condition
		// The session has timed out; give user opportunity to re-log

		final MessageBox mb = new MessageBox();
		mb.setTitle("Perceptive Licensing Server");
		mb.setType(MessageBoxType.ALERT);
		mb.setIcon(MessageBox.ERROR);
		mb.setMessage(exceptionMessage);
		mb.setModal(true);
		mb.addCallback(new CallbackErrorAcknowledger());
		mb.show();

	}

	private class CallbackErrorAcknowledger implements Listener<MessageBoxEvent>
	{
		public void handleEvent(final MessageBoxEvent be)
		{
			// if the error is severe enough to require the user to login again,
			// redirect them to the login page; this will happen after the
			// user acknowledges the dialog. Therefore its important for the
			// dialog to warn the user that this will happen.
		}
	}
	
}
